package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blog.dao.mapper.ArticleTagMapper;
import com.example.blog.dao.mapper.TagMapper;
import com.example.blog.entity.Tag;
import com.example.blog.service.TagService;
import com.example.blog.vo.Result;
import com.example.blog.vo.TagDetailVo;
import com.example.blog.vo.TagVo;
import com.example.blog.entity.ArticleTag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private ArticleTagMapper articleTagMapper;

    @Override
    public Result hots(Long limit) {
        /**
         * 1.标签所拥有的数量最多的标签为 最热标签
         * 2.查询ms_article_tag，根据tag_id进行分组，并根据获得的每个分组的个数，再从大到小排列，取前limit个
         * select count(*) as count,tag_id from ms_article_tag group by tag_id ORDER BY count DESC LIMIT 6
         * 3.由于需要的是前limit个tag_id，所以count(*)不需要显示，用于排序就可以了
         * select tag_id from ms_article_tag group by tag_id ORDER BY count(*) DESC LIMIT 6
         */
        List<Long> hotsTagIds = tagMapper.findHotsTagIds(limit);
        /**
         * 最终需要的是tagId 和tagName ==>tag对象
         */
        if (CollectionUtils.isEmpty(hotsTagIds)) {
            return Result.success(Collections.emptyList());
        }
        List<Tag> tags = tagMapper.findTagsNameByTagIds(hotsTagIds);
        return Result.success(copyList(tags));
    }

    @Override
    public Result findAll() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Tag::getId, Tag::getTagName);
        List<Tag> tags = tagMapper.selectList(null);
        return Result.success(copyList(tags));
    }


    @Override
    public Result findAllDetail() {
        List<Tag> tags = tagMapper.selectList(null);
        List<TagDetailVo> tagDetailVos = new ArrayList<>();
        for (Tag tag : tags) {
            Long id = tag.getId();
            LambdaQueryWrapper<ArticleTag> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ArticleTag::getTagId, id);
            Integer articlesNumber = articleTagMapper.selectCount(queryWrapper);
            tagDetailVos.add(copyTagDetailList(tag, articlesNumber));
        }
        return Result.success(tagDetailVos);
    }

    private TagDetailVo copyTagDetailList(Tag tag, Integer articlesNumber) {
        TagDetailVo tagDetailVo = new TagDetailVo();
        BeanUtils.copyProperties(tag, tagDetailVo);
        tagDetailVo.setArticlesNumber(articlesNumber);
        return tagDetailVo;
    }

    /**
     * 通过标签id查询标签详情
     *
     * @param id
     * @return
     */
    @Override
    public Result findTagById(Long id) {
        Tag tag = tagMapper.selectById(id);
        LambdaQueryWrapper<ArticleTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleTag::getTagId, id);
        Integer articlesNumber = articleTagMapper.selectCount(queryWrapper);
        return Result.success(copyTagDetailList(tag, articlesNumber));
    }

    @Override
    public List<TagVo> findTagsById(Long articleId) {
        /*mybatisPlus无法进行多表查询*/
        /*通过articleId查询对应的tag*/
        List<Tag> tags = tagMapper.findTagsByArticleId(articleId);
        return copyList(tags);
    }

    public List<TagVo> copyList(List<Tag> tags) {
        List<TagVo> tagVoList = new ArrayList<>();
        for (Tag tag : tags) {
            tagVoList.add(copy(tag));
        }
        return tagVoList;
    }

    public TagVo copy(Tag tag) {
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag, tagVo);
        tagVo.setId(String.valueOf(tag.getId()));
        return tagVo;
    }
}
